package com.htwy.core.model;

import com.jfinal.kit.StrKit;
import com.jfinal.plugin.activerecord.Model;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.TableMapping;
import java.util.List;
import java.util.Map;


public abstract class BaseModel<M extends BaseModel<M>> extends Model<M> {

    public abstract List<M> findByCondition(Map<String,Object> conditions);

    public abstract Page<M> paginateByCondition(Map<String,Object> conditions);

    public abstract Page<M> paginateByCondition(int pageNumber, int pageSize, Map<String,Object> conditions);

    public abstract int deleteByPks(String... pks);

    public String getPrimaryKey(){
        String[] primaryKeys = TableMapping.me().getTable(getUsefulClass()).getPrimaryKey();
        if(primaryKeys.length != 1){
            throw new UnsupportedOperationException("只支持字符串形式的单主键");
        }
        return primaryKeys[0];
    }

    public boolean hasPkValue(){
        if(StrKit.notBlank(getStr(getPrimaryKey()))){
            return true;
        }
        return false;
    }
}
